package io.gitee.zhangbinhub.admin.controller.openinner

import io.gitee.zhangbinhub.acp.boot.exceptions.WebException
import io.gitee.zhangbinhub.acp.boot.log.LogAdapter
import io.gitee.zhangbinhub.admin.api.CommonPath
import io.gitee.zhangbinhub.admin.api.ServerApi
import io.gitee.zhangbinhub.admin.base.BaseController
import io.gitee.zhangbinhub.admin.service.RuntimeConfigService
import io.gitee.zhangbinhub.admin.vo.RuntimeConfigVo
import io.swagger.v3.oas.annotations.*
import io.swagger.v3.oas.annotations.responses.ApiResponse
import io.swagger.v3.oas.annotations.responses.ApiResponses
import io.swagger.v3.oas.annotations.tags.Tag
import jakarta.validation.constraints.NotBlank
import org.springframework.beans.BeanUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

/**
 * @author zhang by 01/02/2019
 * @since JDK 11
 */
@Validated
@RestController
@RequestMapping(CommonPath.openInnerBasePath)
@Tag(name = "运行参数配置（内部开放接口）")
class OpenInnerRuntimeController @Autowired
constructor(
    logAdapter: LogAdapter,
    private val runtimeConfigService: RuntimeConfigService
) : BaseController(logAdapter) {

    @Operation(summary = "获取参数信息", description = "根据参数名称获取")
    @ApiResponses(ApiResponse(responseCode = "400", description = "找不到参数信息；"))
    @GetMapping(value = [ServerApi.runtime + "/{name}"], produces = [MediaType.APPLICATION_JSON_VALUE])
    @Throws(WebException::class)
    fun find(
        @Parameter(description = "参数名称", required = true)
        @NotBlank(message = "参数名称不能为空")
        @PathVariable(name = "name")
        name: String
    ): ResponseEntity<RuntimeConfigVo> =
        (runtimeConfigService.findByName(name) ?: throw WebException("找不到参数信息")).let {
            RuntimeConfigVo().apply {
                BeanUtils.copyProperties(it, this)
            }.let {
                ResponseEntity.ok(it)
            }
        }

    @Operation(summary = "获取参数信息", description = "根据参数名称获取")
    @GetMapping(value = [ServerApi.runtime], produces = [MediaType.APPLICATION_JSON_VALUE])
    @Throws(WebException::class)
    fun findList(): ResponseEntity<Map<String, RuntimeConfigVo>> = ResponseEntity.ok(runtimeConfigService.findAll())

}
